package Metadata

import (
	"encoding/json"
	"fmt"
	"time"
)

type Metadata struct {
	Name        string    `json:"name" xml:"name"`
	Description string    `json:"description" xml:"description"`
	CID         string    `json:"cid" xml:"cid"`
	Filetype    string    `json:"filetype" xml:"filetype"`
	Timestamp   time.Time `json:"timestamp" xml:"timestamp"`
}

// from struct to json
func (md Metadata) MarshalMetadata() []byte {
	data, err := json.Marshal(&md)
	if err != nil {
		fmt.Println("Marshal Metadata Error")
	}
	return data
}

// from json to struct
func (md Metadata) UnMarshalMetadata(str []byte) (e error) {
	err := json.Unmarshal(str, &md)
	if err != nil {
		return err
	}
	return nil
}

// check whether the content is complete
func (md Metadata) check() (b bool) {
	// check file type
	if md.Filetype != "text" && md.Filetype != "picture" && md.Filetype != "video" {
		return false
	}
	// check description
	if md.Description == "" {
		return false
	}
	return true
}
